//
//  TabbarController.m
//  NemoSDKDemo
//
//  Created by 杨旭东 on 2016/12/5.
//  Copyright © 2016年 JackYang. All rights reserved.
//

#import "TabbarController.h"
#import "XYAlertView.h"

@interface TabbarController () <NemoSDKDelegate>

@end

@implementation TabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [XYHUDManager showLoading];
    [NemoSDK sharedInstance].delegate = self;
}

- (void)showMeetingUI {
    self.meetingVC = [[XYMeetingViewController alloc] init];
    self.meetingVC.modalPresentationStyle = UIModalPresentationFullScreen;
    [self presentViewController:self.meetingVC animated:YES completion:nil];
    [self.meetingVC meetingState:NemoCallState_Connecting];
}


#pragma mark NemoSDKDelegate

- (void)loginResult:(XYUser *)user success:(BOOL)success {
    [XYHUDManager hideToast];
    if (success) {
        [XYSDKManager sharedInstance].user = user;
    }else{
        [XYHUDManager showBriefToast:@"登录失败"];
    }
}

- (void)nemoSDKDidCall:(NSString *)number failed:(NemoCallFailedReason)reason {
    if ([number isEqualToString:[XYSDKManager sharedInstance].meetingNumber]) {
        if (reason == NemoCallFailedReason_InvalidPassword) {
            [self.meetingVC meetingState:NemoCallState_DisConnected];
            self.meetingVC = nil;
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                XYAlertView *alert = [XYAlertView alertType:XYAlertTypePassword];
                [alert show:self];
                alert.inputText = ^(NSString * _Nonnull text) {
                    [[XYSDKManager sharedInstance] joinMeeting:number password:text];
                    [self showMeetingUI];
                };
            });
        }
    }
}

- (void)nemoSDKDidCall:(NSString *)number stateChanged:(NemoCallState)callState reason:(NSString *)reason {
    [self.meetingVC meetingState:callState];
    if (callState == NemoCallState_DisConnected) {
        self.meetingVC = nil;
    }
}

- (void)nemoSDKDidRosterChanged:(NemoRosters *)rosters {
    [self.meetingVC rosterChanged:rosters];
}

- (void)nemoSDKDidVideoChanged:(NSArray<NemoLayout *> *)videos {
    [self.meetingVC videoViewLayout:videos];
}

- (void)nemoSDKDidConferenceManagementStateChanged:(NemoConferenceManagerManagementType)type confMgmtInfo:(NemoConfMgmtInfo *)info {
    [self.meetingVC meetingControlChanged:type confMgmtInfo:info];
}

- (void)nemoSDKWhiteboardDidStateChanged:(NemoWhiteboardRemoteState)remoteState reason:(NSString *)reason {
    [self.meetingVC whiteboardStateChanged:remoteState];
}

- (void)nemoSDKWhiteboardDidReceieved:(id)remoteMessage type:(NemoWhiteboardOperationState)type {
    [self.meetingVC whiteboardRemoteMessage:remoteMessage type:type];
}

- (void)contentShareStateChanged:(NemoShareContentType)type state:(NemoShareContentState)state {
    [self.meetingVC contentShareStateChanged:type state:state];
}

- (void)nemoSDKDidUploadLogFileErrorInfo:(NSString *)errorInfo {
    [XYHUDManager hideToast];
    [XYHUDManager showBriefToast:@"上传成功"];
}

- (void)nemoSDKDidRecieveCaptionDisplay:(NemoCaptionInfoModel *)captionInfo {
    [self.meetingVC recieveCaption:captionInfo];
}

- (void)nemoSDKDidDualStreamStateChanged:(NemoDualState)state reason:(NSString *)reason {
    [self.meetingVC dualStreamStateChanged:state reason:reason];
}

- (void)nemoSDKDidRecordingStateChanged:(BOOL)start {
    [self.meetingVC recordingStateChanged:start];
}

- (void)nemoSDKDidReceiveCall:(NSString *)number displayName:(NSString *)displayName {
    __weak typeof(self)ws = self;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"来电" message:displayName preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *reject = [UIAlertAction actionWithTitle:@"拒绝" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        if (![XYSDKManager sharedInstance].isComingCallDispose) {
            [[XYSDKManager sharedInstance] rejectMeeting];
        }
    }];
    
    UIAlertAction *agree = [UIAlertAction actionWithTitle:@"接受" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (![XYSDKManager sharedInstance].isComingCallDispose) {
            if (self.meetingVC) {
                [[XYSDKManager sharedInstance] leaveMeeting];
                //等待UI切换完成
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [[XYSDKManager sharedInstance] acceptMeeting];
                    [ws showMeetingUI];
                });
            }else {
                [[XYSDKManager sharedInstance] acceptMeeting];
                [ws showMeetingUI];
            }
        }
    }];
    [alert addAction:reject];
    [alert addAction:agree];
    if (self.meetingVC) {
        [self.meetingVC presentViewController:alert animated:YES completion:nil];
    }else {
        [self presentViewController:alert animated:YES completion:nil];
    }
}

@end
